package com.haoyeteng.sxbd.modular.business.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.haoyeteng.sxbd.core.base.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import com.haoyeteng.sxbd.core.base.model.ResponseResult;
import com.haoyeteng.sxbd.core.common.annotion.Permission;
import com.haoyeteng.sxbd.core.common.constant.factory.PageFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.haoyeteng.sxbd.core.log.LogObjectHolder;
import com.haoyeteng.sxbd.core.shiro.ShiroKit;

import org.springframework.web.bind.annotation.RequestParam;
import com.haoyeteng.sxbd.modular.business.model.Contract;
import com.haoyeteng.sxbd.modular.business.model.OrderDtl;
import com.haoyeteng.sxbd.modular.business.service.IContractService;
import com.haoyeteng.sxbd.modular.business.service.IOrderDtlService;

/**
 * 合同管理控制器
 *
 * @author haoyeteng
 * @Date 2019-10-04 22:48:31
 */
@Controller
@RequestMapping("/contract")
public class ContractController extends BaseController {

    private String PREFIX = "/business/contract/";

    @Autowired
    private IContractService contractService;
    
    @Autowired
	private IOrderDtlService orderDtlService;

    /**
     * 跳转到合同管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "contract.html";
    }

    /**
     * 跳转到添加合同管理
     */
    @RequestMapping("/contract_add")
    public String contractAdd() {
        return PREFIX + "contract_add.html";
    }

    /**
     * 跳转到修改合同管理
     */
    @RequestMapping("/contract_update/{contractId}")
    public String contractUpdate(@PathVariable Integer contractId, Model model) {
        Contract contract = contractService.selectById(contractId);
        model.addAttribute("item",contract);
        LogObjectHolder.me().set(contract);
        return PREFIX + "contract_edit.html";
    }

    /**
     * 获取合同管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public ResponseResult<Object> list(String contractStatus, String contractNo, String orderId, String applyTime) {
    	ResponseResult<Object> result = new ResponseResult<Object>();
    	Page<Contract> page = new PageFactory<Contract>().defaultPage();
    	List<Map<String, Object>> contractList = contractService.selectContractList(page, contractStatus, contractNo, orderId, applyTime);
        result.setData(contractList);
 		result.setCount(page.getTotal());
		result.setCode(SUCCESS_TIP.getCode());
     	result.setMsg(SUCCESS_TIP.getMessage());
 		return result;
    }

    /**
     * 新增合同管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(Contract contract) {
        contractService.insert(contract);
        return SUCCESS_TIP;
    }

    /**
     * 删除合同管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission
    public Object delete(@RequestParam Integer contractId) {
        contractService.deleteById(contractId);
        return SUCCESS_TIP;
    }

    /**
     * 修改合同管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(Integer contractId) {
    	Contract contractTemp = contractService.selectById(contractId);
		contractTemp.setStatus(2);
		contractService.updateById(contractTemp);
        return SUCCESS_TIP;
    }

    /**
     * 合同管理详情
     */
    @RequestMapping(value = "/detail/{contractId}")
    @ResponseBody
    public Object detail(@PathVariable("contractId") Integer contractId) {
        return contractService.selectById(contractId);
    }
    
    
    @RequestMapping(value = "/download", method = {RequestMethod.POST, RequestMethod.GET},
            produces = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Permission
    public ResponseEntity<?> download(Integer contractId){
    	System.out.println("==============="+contractId);
        try {
        	Contract contract = contractService.selectById(contractId);
        	List<OrderDtl> orderDtlList = orderDtlService.selectList(new EntityWrapper<OrderDtl>().eq("order_id", contract.getOrderId()));
            ResponseEntity<?> responseEntity = contractService.exportPdf(contract, orderDtlList);
            return responseEntity;
        } catch (Exception e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        return new ResponseEntity<String>("{ \"code\" : \"404\", \"message\" : \"not found\" }",
                headers, HttpStatus.NOT_FOUND);
    }
}
